package com.dao.impl;

import com.dao.EmployeesDao;
import com.entity.Employees;
import com.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
 *
 *@author XCH
 *@date2025 - 02 - 24
 *
 */
public class EmployeesDaoImpl implements EmployeesDao {
    @Override
    public List<Employees> selectByPage(int currentPage, int pageSize) {
        List<Employees> list = new ArrayList<>();
        String sql = "select id, name, age, phone, sta_pos, add_time, update_time from staffs limit ?, ?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, (currentPage - 1) * pageSize);
            ps.setInt(2, pageSize);
            System.out.println(ps.toString());
            ResultSet rst = ps.executeQuery();
            while (rst.next()) {
                Employees employees = new Employees();
                employees.setId(rst.getString("id"));
                employees.setName(rst.getString("name"));
                employees.setAge(rst.getInt("age"));
                employees.setPhone(rst.getString("phone"));
                employees.setSta_pos(rst.getString("sta_pos"));
                employees.setAdd_time(rst.getDate("add_time"));
                employees.setUpdate_time(rst.getDate("update_time"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return List.of();
    }

    @Override
    public int insert(Employees employees) {
        int result = 0;
        String sql = "insert into staffs(name, age, phone, sta_pos) values(?,?,?,?)";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, employees.getName());
            ps.setInt(2, employees.getAge());
            ps.setString(3, employees.getPhone());
            ps.setString(4, employees.getSta_pos());
            System.out.println(ps.toString());
            result = ps.executeUpdate();
            System.out.println(result);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(null, ps, conn);
        }
        return result;
    }

    @Override
    public int delete(String id) {
        int result = 0;
        String sql = "delete from staffs where id = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(null, ps, conn);
        }
        return result;
    }

    @Override
    public int update(Employees employees) {
        int result = 0;
        String sql = "update staffs set name = ?, age = ?, phone = ?, sta_pos = ? where id = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, employees.getName());
            ps.setInt(2, employees.getAge());
            ps.setString(3, employees.getPhone());
            ps.setString(4, employees.getSta_pos());
            ps.setString(5, employees.getId());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(null, ps, conn);
        }
        return result;
    }
}
